<template>
  <div class="aa">
    <h3>用户信息管理</h3>
    <br /><br /><br /><br /><br /><br /><br /><br />
    <el-avatar :src="userlist.portrait_img"></el-avatar><br />
    <div class="bb">
      <el-form :model="userlist" label-width="100px">
        <el-form-item label="账号" prop="username" class="aa">
          <el-input type="username" v-model="userlist.username" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="网名" prop="username" class="aa">
          <el-input type="username" v-model="userlist.account" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" class="aa">
          <el-input type="password" v-model="userlist.password" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex" class="aa">
          <el-input v-model="userlist.sex" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile" class="aa">
          <el-input v-model="userlist.mobile" :disabled="true"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age" class="aa">
          <el-input v-model="userlist.age" :disabled="true"></el-input>
        </el-form-item>
      </el-form>
      <el-button type="warning" round @click="upd">修改信息</el-button>
      <a href="/">退出登录</a>
    </div>

    <el-dialog title="修改信息" :visible.sync="dialogFormVisible">
      <el-form>
        <el-form-item label="头像" prop="portrait_img" class="aa">
          <el-avatar :src="userlist.portrait_img"></el-avatar>
          <el-upload
            action="http://up-z2.qiniup.com"
            :on-success="uploadSuccess"
            :auto-upload="true"
            :data="upload_data"
            :on-error="uploadError"
          >
            <el-button size="small" type="primary">点击选择图片 </el-button>
          </el-upload>
        </el-form-item>
        <el-form-item label="账号">
          <el-input autocomplete="off" v-model="userlist.username"></el-input>
        </el-form-item>
        <el-form-item label="网名">
          <el-input autocomplete="off" v-model="userlist.account"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password" class="aa">
          <el-input type="username" v-model="userlist.password" show-password></el-input>
        </el-form-item>
        <el-form-item label="性别">
          <el-input autocomplete="off" v-model="userlist.sex"></el-input>
        </el-form-item>
        <el-form-item label="手机号">
          <el-input autocomplete="off" v-model="userlist.mobile"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age" class="aa">
          <el-input v-model="userlist.age"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="udd">确定修改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import Axios from "axios";
export default {
  name: "",
  data() {
    return {
      userlist: {
        id: 0,
        username: "",
        account:'',
        mobile: "",
        password:"",
        sex: "",
        portrait_img: "",
        age: "",
      },
      dialogFormVisible: false,
      upload_data: {
        token: "",
      },
      qiniu_token: "",
      url: "",
      uid:'',
    };
  },
  props: {},

  components: {},

  created() {},

  mounted() {
    // 获取当前登录用户信息
    Axios.get("http://127.0.0.1:8000/user/infolist/", {
      headers: { token: localStorage.getItem("token") || "" },
    }).then((res) => {
      this.uid = res.data.userinfo.id;
      console.log("获取信息>>", res);
      this.userlist = res.data.userinfo;
    });
    // 获取七牛云token
    Axios.get("http://127.0.0.1:8000/user/qiniu_token/").then((res) => {
      console.log("获取七牛云token>>", res);
      this.qiniu_token = res.data.token;
      this.upload_data.token = res.data.token;
    });
  },

  methods: {
    // 上传成功的钩子方法
    uploadSuccess(res, files) {
      console.log("上传图片成功的响应>>>", res);
      // console.log("上传的文件", files);
      // 图片地址        图片文件路径                图片名
      this.url = "http://rg0riamy6.hn-bkt.clouddn.com/" + res.key;
      this.userlist.portrait_img = this.url;
      console.log("整体的图片地址", this.url);
    },
    // 文件上传失败时的钩子
    uploadError(err) {
      console.log(err);
      this.$message({
        type: "error",
        message: err.response.data.msg,
      });
    },
    // 点击修改按钮弹出修改框
    upd(id) {
      console.log("ididid",id)
      this.dialogFormVisible = true;
    },
    // 修改用户信息
    udd() {
      this.dialogFormVisible = false;
      Axios.put(
        "http://127.0.0.1:8000/user/userinfo/" + this.uid + '/',
          // uid:this.uid,
          this.userlist
        )
        .then((res) => {
          console.log("修改用户的响应>>", res);
          if (res.data.code == 200) {
            this.$notify({
              title: "成功",
              message: "修改用户信息成功！",
              type: "success",
            });
          } else {
            this.$notify.error({
              title: "错误",
              message: res.statusText,
            });
          }
        })
        .catch((err) => {
          console.log(err);
        });
    },
  },

  computed: {},

  watch: {},

  directives: {},

  filters: {},
};
</script>

<style>
* {
  margin: 0;
}
.aa {
  width: 400px;
}
.bb {
  width: 500px;
  height: 500px;
}
</style>
